package com.market.service.impl;

import com.github.pagehelper.PageHelper;
import com.market.common.exception.ServiceException;
import com.market.common.vo.ResponseCode;
import com.market.entity.Worker;
import com.market.mapper.WorkerMapper;
import com.market.service.WorkerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author :  KanShan
 * @version : 1.8
 * @Date: 2021/11/29/19:45
 * @注释 :
 */
@Service
public class WorkerServiceImpl implements WorkerService {
    @Resource
    WorkerMapper workerMapper;


    @Override
    public List<Worker> findWorker() {

        List<Worker> selectList = workerMapper.selectList();

        return selectList;
    }

    @Override
    public List<Worker> listWorkerById() {

        return null;
    }

    @Override
    public List<Worker> selectLisByName(String name) {
        List<Worker> workers = workerMapper.selectLisByName(name);
        return workers;
    }

    @Override
    public int register(Worker worker) throws SecurityException {
        int count = 0;
        List<Worker> selectList = workerMapper.selectList();
        for (Worker workers : selectList) {
            if (workers.getWorkerName() != worker.getWorkerName()) {
                int insert = workerMapper.insert(worker);
                if (insert > 0) {
                    count = insert;
                    throw new ServiceException(ResponseCode.SUCCESS);
                } else {
                    throw new ServiceException(ResponseCode.ERROR);
                }
            } else {
                throw new ServiceException(ResponseCode.ERROR);
            }
        }
        return count;
    }

    @Override
    public int deleteWorker(Long id) {
        try {
            return workerMapper.deleteWorker(id);
        } catch (Exception e) {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }

    @Override
    public int updateWorker(Worker worker) {
        int i = workerMapper.updateWorker(worker);
        if (i > 0) {
            return i;
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }

    @Override
    public int updateWorkerPassword(Integer workerId, String password) {
        int i = workerMapper.updateWorkerPassword(workerId, password);
        if (i > 0) {
            return i;
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }

    }

    @Override
    public Worker login(Long workerId, String password) throws ServiceException {
        Worker worker = workerMapper.selectLogin(workerId);
        if (worker != null) {
            if (worker.getPassword().equals(password)) {
                throw new ServiceException(ResponseCode.SUCCESS);
            } else {
                throw new ServiceException(ResponseCode.ERROR);
            }
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }

}
